package com.example.demo.dao;

import com.example.demo.pojo.Lend;
import com.example.demo.pojo.QueryCriteria;
import com.example.demo.pojo.Reservation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface LendMapper {

    @Insert("insert into reservation(book_id,reader_id,reservation_date,reservatiion_state) values(#{book_id},#{reader_id},#{reservation_date},#{reservatiion_state})")
    int insertReservation(Reservation reservation);

    @Insert("insert into lend_list(book_id,reader_id,lend_date,back_date,finenum,is_return,fine)" +
            " values(#{book_id},#{reader_id},#{lend_date},#{back_date},#{finenum},#{is_return},#{fine})")
    int insertLendList(Lend lend);

    @Select("select * from reservation where book_id = #{book_id} and reader_id = #{reader_id}")
    Reservation selReservation(Reservation reservation);

    List<Reservation> selAllReservation();

    @Select("select count(*) from reservation")
    int selAllReservationCount();


    @Select("select DATEDIFF(now(),#{back_date}) from lend_list where ser_num = #{ser_num}")
    int selLendListBackDateToNowDayNum(int ser_num,Date back_date);

    @Select("select hour(TIMEDIFF(now(),#{date}))")
    int reservationTimeToNowDayNum(Date date);

    @Select("select * from lend_list where ser_num = #{ser_num} and reader_id = #{reader_id}")
    Lend selLendListBySerNum(int ser_num,int reader_id);

    List<Lend> selFineHistory();

    @Select("select count(*) from lend_list where finenum > 0")
    int selFineHistoryCount();

    @Select("select count(*) from lend_list")
    int selLendListCount();

    @Select("select count(*) from lend_list where reader_id = #{reader_id}")
    int selLendListCountByReaderId(int reader_id);


    @Update("update reservation set reservatiion_state = #{k} where book_id = #{book_id} and reader_id = #{reader_id}")
    void setReservationState(int k,int book_id,int reader_id);

    @Update("update lend_list set is_return = #{is_return} , back_date = #{back_date} where ser_num = #{ser_num}")
    void updateLendListBackTimeAndIsreturnbySerNum(Lend lend);

    @Update("update lend_list set finenum = #{finenum},fine = #{fine} where ser_num = #{ser_num}")
    void updateLendListFinenumAndFineBySerNum(int ser_num, BigDecimal finenum, int fine);

    @Update("update lend_list fine = #{fine} where ser_num = #{ser_num}")
    void updateLendListFineBySerNum(int ser_num,int fine);

    @Update("update lend_list set back_date = #{back_date} where ser_num = #{ser_num}")
    void renewBookBySerNum(int ser_num,Date back_date);

    @Select("select * from lend_list")
    List<Lend> selLendList();

    List<Lend> selAllLendList();

    List<Lend> selAllReaderLendList(int reader_id);

    List<Lend> selLendListByConditions(QueryCriteria queryCriteria);

    int selLendListCountByConditions(QueryCriteria queryCriteria);




}
